package com.skyc.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/PhoneServlet")
public class PhoneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String phone=request.getParameter("phone");
		
		InputStream in = null;
		InputStreamReader reader = null;
		URLConnection con = null;
		BufferedReader br = null;
		
		String str = "http://apis.juhe.cn/mobile/get?phone="+phone+"&dtype=&key=4081d1f3fe5b12b807a866d58fe63ac2";
		String result = "";
		try {
			URL url = new URL(str);
			con = url.openConnection();
			in = con.getInputStream();
			reader = new InputStreamReader(in, "utf-8");
			br = new BufferedReader(reader);
			String s1 = "";
			while ((s1 = br.readLine()) != null) {
				result += s1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null)
				reader.close();
			if (in != null)
				in.close();
		}
        System.out.println(result);
		out.print(result);
		out.flush();
		out.close();
	}
}
